//------------------------------------------------------------------------------
// <copyright file="ErrorReportComponent.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.ServiceModel;
using DotBeer.Business.Entities;
using System.Reflection;
using DotBeer.Business.Entities.ErrorReporting;

namespace DotBeer.Business.Components
{
    public class ErrorReportComponent
    {
        #region private members

        private ErrorReportingServiceClient errorReportClient;

        #endregion

        #region properties

        public event EventHandler<ReportErrorCompletedEventArgs> ReportErrorCompleted;

        #endregion

        #region constructors

        public ErrorReportComponent()
        {
        }

        #endregion

        #region public methods

        public void ReportErrorAsync(Exception exception, string additionalInfo)
        {
            if (this.errorReportClient == null)
            {
                this.errorReportClient = new ErrorReportingServiceClient();
                this.errorReportClient.ReportErrorCompleted += new EventHandler<ReportErrorCompletedEventArgs>(errorReportClient_ReportErrorCompleted);
            }
            
            this.errorReportClient.ReportErrorAsync(String.Format("DotBeer v{0}", Assembly.GetExecutingAssembly().GetName().Version),
                                                    exception.Message,
                                                    exception.StackTrace,
                                                    additionalInfo);
        }

        #endregion

        #region private methods

        private void errorReportClient_ReportErrorCompleted(object sender, ReportErrorCompletedEventArgs e)
        {
            if (this.ReportErrorCompleted != null)
                this.ReportErrorCompleted(this, e);
        }

        #endregion
    }
}
